﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using Contracts.Entities;

namespace Model.MovieElements
{
    class ActorConverter
    {
        /// <summary>
        /// Für das Lesen einer MovieActors-Relation
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(EntityCollection<Actors> actors)
        {
            var results = new List<MovieElement>();
            foreach (var entity in actors)
                results.Add(GetElement(entity));

            return results;
        }

        /// <summary>
        /// Für das Lesen aller Actors aus Datenbank
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(ObjectSet<Actors> actors)
        {
            var results = new List<MovieElement>();
            foreach (var entity in actors)
                results.Add(GetElement(entity));

            return results;
        }

        /// <summary>
        /// Für das Schreiben der Actors aus Tmdb-Quelle
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(IEnumerable<TmdbPerson> actors)
        {
            var items = new List<MovieElement>();

            foreach (var item in actors)
            {
                items.Add(new MovieElement()
                {
                    Name = item.Name,
                    Type = MovieElementType.Actor
                });
            }

            return items;
        }

        /// <summary>
        /// Für das Schreiben der Actors aus eigener Quelle
        /// </summary>
        public static EntityCollection<Actors> GetEntities(IEnumerable<MovieElement> actors)
        {
            var results = new EntityCollection<Actors>();

            foreach (var item in actors)
                results.Add(GetEntity(item));

            return results;
        }

        public static MovieElement GetElement(Actors actor)
        {
            return new MovieElement()
            {
                Id = actor.Id,
                Name = actor.Name,
                Type = MovieElementType.Actor
            };
        }

        public static Actors GetEntity(MovieElement actor)
        {
            return new Actors()
            {
                Id = actor.Id,
                Name = actor.Name
            };
        }
    }
}
